package org.light.utils;

import java.util.ArrayList;
import java.util.List;

import org.light.core.Writeable;
import org.light.domain.Statement;
import org.light.domain.Util;
public class MyConfigurationSourceGenerator extends Util{
	public MyConfigurationSourceGenerator(){
		super();
		this.fileName = "MyConfigurationSource.java";
		this.standardName = "MyConfigurationSource";
	}
	
	public MyConfigurationSourceGenerator(String packageToken){
		super();
		this.packageToken = packageToken;
		this.fileName = "MyConfigurationSource.java";
		this.standardName = "MyConfigurationSource";
	}
	
	@Override
	public void setPackageToken(String packageToken){
		this.packageToken = packageToken;
	}
	
	@Override
	public String generateUtilString() {
		List<Writeable> sList = new ArrayList<Writeable>();
		sList.add(new Statement(1000L,0,"package "+this.packageToken+".utils;"));
		sList.add(new Statement(2000L,0,""));
		sList.add(new Statement(3000L,0,"import java.util.Arrays;"));
		sList.add(new Statement(4000L,0,"import java.util.List;"));
		sList.add(new Statement(5000L,0,""));
		sList.add(new Statement(6000L,0,"import javax.servlet.http.HttpServletRequest;"));
		sList.add(new Statement(7000L,0,""));
		sList.add(new Statement(8000L,0,"import org.springframework.web.cors.CorsConfiguration;"));
		sList.add(new Statement(9000L,0,"import org.springframework.web.cors.CorsConfigurationSource;"));
		sList.add(new Statement(10000L,0,"import org.springframework.web.cors.UrlBasedCorsConfigurationSource;"));
		sList.add(new Statement(11000L,0,"import org.springframework.web.filter.CorsFilter;"));
		sList.add(new Statement(12000L,0,""));
		sList.add(new Statement(13000L,0,"public class MyConfigurationSource implements CorsConfigurationSource{"));
		sList.add(new Statement(14000L,0,""));
		sList.add(new Statement(15000L,1,"@Override"));
		sList.add(new Statement(16000L,1,"public CorsConfiguration getCorsConfiguration(HttpServletRequest arg0) {"));
		sList.add(new Statement(17000L,2,"//创建CorsConfiguration对象后添加配置"));
		sList.add(new Statement(18000L,2,"CorsConfiguration config = new CorsConfiguration();"));
		sList.add(new Statement(19000L,2,""));
		sList.add(new Statement(20000L,2,"List<String> allowedHeaders = Arrays.asList(\"x-token\", \"content-type\", \"X-Requested-With\", \"XMLHttpRequest\");"));
		sList.add(new Statement(21000L,2,"List<String> exposedHeaders = Arrays.asList(\"x-token\", \"content-type\", \"X-Requested-With\", \"XMLHttpRequest\","));
		sList.add(new Statement(22000L,4,"\"Referrer Policy:no-referrer-when-downgrade\");"));
		sList.add(new Statement(23000L,2,"List<String> allowedMethods = Arrays.asList(\"POST\", \"GET\", \"DELETE\", \"PUT\", \"OPTIONS\");"));
		sList.add(new Statement(24000L,2,"List<String> allowedOrigins = Arrays.asList(\"*\");"));
		sList.add(new Statement(25000L,2,"config.setAllowedHeaders(allowedHeaders);"));
		sList.add(new Statement(26000L,2,"config.setAllowedMethods(allowedMethods);"));
		sList.add(new Statement(27000L,2,"config.setAllowedOrigins(allowedOrigins);"));
		sList.add(new Statement(28000L,2,"config.setExposedHeaders(exposedHeaders);"));
		sList.add(new Statement(29000L,2,"config.setMaxAge(36000L);"));
		sList.add(new Statement(30000L,2,"config.setAllowCredentials(true);"));
		sList.add(new Statement(31000L,0,"//		        //设置放行哪些原始域"));
		sList.add(new Statement(32000L,0,"//		        config.addAllowedOrigin(\"*\");"));
		sList.add(new Statement(33000L,0,""));
		sList.add(new Statement(34000L,2,"//是否发送Cookie"));
		sList.add(new Statement(35000L,2,"//config.setAllowCredentials(true);"));
		sList.add(new Statement(36000L,0,""));
		sList.add(new Statement(37000L,2,"//2. 添加映射路径"));
		sList.add(new Statement(38000L,2,"UrlBasedCorsConfigurationSource corsConfigurationSource ="));
		sList.add(new Statement(39000L,4,"new UrlBasedCorsConfigurationSource();"));
		sList.add(new Statement(40000L,2,"corsConfigurationSource.registerCorsConfiguration(\"/**\", config);"));
		sList.add(new Statement(41000L,2,"//返回CorsFilter"));
		sList.add(new Statement(42000L,2,"return config;"));
		sList.add(new Statement(43000L,1,"}"));
		sList.add(new Statement(44000L,1,""));
		sList.add(new Statement(45000L,0,"}"));
		
		return WriteableUtil.merge(sList).getContent();
	}
}